package labeler {
	import com.adobe.serialization.json.JSON;
	
	import flare.animate.Easing;
	import flare.animate.Transitioner;
	import flare.display.RectSprite;
	import flare.util.Filter;
	import flare.util.IEvaluable;
	import flare.util.Property;
	import flare.util.Shapes;
	import flare.vis.data.Data;
	import flare.vis.data.DataSprite;
	import flare.vis.operator.Operator;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	import flash.net.URLRequest;
	
	import render.IconRenderer;

	public class OnlineStatusLabeler extends Operator {
		/** Constant indicating that labels be placed in their own layer. */
		public static const LAYER:String = "layer";
		/** Constant indicating that labels be added as children. */
		public static const CHILD:String = "child";
		
		/** @private */
		protected var _policy:String;
		/** @private */
		protected var _labels:Sprite;
		/** @private */
		protected var _group:String;
		/** @private */
		protected var _filter:Function;
		/** @private */
		protected var _source:Property;
		/** @private */
		protected var _access:Property = Property.$("props.online");
		
		/** @private */
		protected var _t:Transitioner;
		
		public var xOffset:Number = 0;
		/** The default <code>y</code> value for labels. */
		public var yOffset:Number = 0;
		
		/** The name of the property in which to store created labels.
		 *  The default is "props.label". */
		public function get access():String { return _access.name; }
		public function set access(s:String):void { _access = Property.$(s); }
		
		/** The name of the data group to label. */
		public function get group():String { return _group; }
		public function set group(g:String):void { _group = g; setup(); }
		
		/** The source property that provides the label text. This
		 *  property will be ignored if the <code>textFunction<code>
		 *  property is non-null. */
		public function get source():String { return _source.name; }
		public function set source(s:String):void { _source = Property.$(s); }
		
		/** Boolean function indicating which items to process. Only items
		 *  for which this function return true will be considered by the
		 *  labeler. If the function is null, all items will be considered.
		 *  @see flare.util.Filter */
		public function get filter():Function { return _filter; }
		public function set filter(f:*):void { _filter = Filter.$(f); }
		
		/** Optional function for determining label text. */
		public var iconFunction:Function = null;

		/** The policy for how labels should be applied.
		 *  One of LAYER (for adding a separate label layer) or
		 *  CHILD (for adding labels as children of data objects). */
		public function get labelPolicy():String { return _policy; }
		
		/** A sprite containing the labels, if a layer policy is used. */
		public function get labels():Sprite { return _labels; }
		
		public function OnlineStatusLabeler(source:*=null, group:String=Data.NODES,
				filter:*=null, policy:String=LAYER) {
			if (source is String) {
				_source = Property.$(source);
			} else if (source is Property) {
				_source = Property(source);
			} else if (source is IEvaluable) {
				iconFunction = IEvaluable(source).eval;
			} else if (source is Function) {
				iconFunction = source;
			}
			_policy = policy;
			
			_group = group;
			this.filter = filter;
			
		}
		
		/** @inheritDoc */
		public override function setup():void {
			if (_policy == LAYER) {
				_labels = visualization.labels;
				if (_labels == null) {
					_labels = new Sprite();
					_labels.mouseChildren = false;
					_labels.mouseEnabled = false;
					visualization.labels = _labels;
				}
			}
		}
		
		/** @inheritDoc */
		public override function operate(t:Transitioner=null):void {
			// new Transitioner
//			_t = (t ? t : Transitioner.DEFAULT);
			_t = (t ? t : getDefaultTransitioner());
			visualization.data.visit(process, group, filter);
			_t = null;
		}
		
		public function getDefaultTransitioner():Transitioner {
			var t:Transitioner = new Transitioner(1.5, Easing.easeInOutBounce);
			return t;
		}
		
		/**
		 * Performs label creation and layout for the given data sprite.
		 * Subclasses should override this method to perform custom labeling. 
		 * @param d the data sprite to process
		 */
		protected function process(d:DataSprite):void {
			var label:RectSprite = getRect(d, true);
			var status:Boolean = getOnlineStatus(d);
			var o:Object, x:Number, y:Number, a:Number, v:Boolean;
			if (_policy == LAYER) {
				o = _t.$(d);
				if (o.shape == Shapes.BLOCK) { 	
					x = o.u + o.w/2;
					y = o.v + o.h/2;
				} else {
					x = o.x;
					y = o.y;
				}
				a = o.alpha;
				v = o.visible;
				o = _t.$(label);
				o.x = x + xOffset;
				o.y = y + yOffset;
				o.alpha = a;
				o.visible = v;
                                if (status) {
                                  o.fillColor = 0xff00ff00;
                                } else {
                                  o.fillColor = 0xffaaaaaa;
                                }
			} else {
				o = _t.$(label);
				o.x = xOffset;
				o.y = yOffset;
                                if (status) {
                                  o.fillColor = 0xff00ff00;
                                } else {
                                  o.fillColor = 0xffaaaaaa;
                                }
			}
			label.render();
		}
		
		protected function getRect(d:DataSprite, create:Boolean=false, visible:Boolean=true):RectSprite {
			var label:RectSprite = _access.getValue(d);
			var size:Number = 3;
			if (!label && !create) {
				return null;
			} else if (!label) {
				label = new RectSprite(-size, -size, 2*size, 2*size);
                                label.fillColor = 0xff00ff00;
				label.visible = visible;
				_access.setValue(d, label);
//				xOffset = -size;
//				yOffset = -size;

//				label.filters = [filter1];		
				
				if (_policy == LAYER) {
					_labels.addChild(label);
					label.x = d.x + xOffset;
					label.y = d.y + yOffset;
				} else {
					d.addChild(label);
					label.mouseEnabled = false;
					label.mouseChildren = false;
					label.x = xOffset;
					label.y = yOffset;
				}
			}
			return label;
		}
		protected function getOnlineStatus(d:DataSprite):Boolean {
			if (iconFunction != null) {
				return iconFunction(d);
			} else {
				return _source.getValue(d);
			}
		}
	}
}
